package accounting_system;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.ScrollPaneLayout;

public class MainFrame extends JFrame implements WindowListener, ActionListener{
    private String[][] persons;
    private MainTable table = new MainTable();
    String[] columnsName = new String[]{"Фамилия", "Имя", "Отчество", "Должность"};
    
    public MainFrame(){
        super("Кадровая структура");
        
        JScrollPane scroll = new JScrollPane(table);
        
        JButton addButton = new JButton("Добавить");
        JButton editButton = new JButton("Изменить");
        JButton removeButton = new JButton("Удалить");
        JButton exitButton = new JButton("Выход");
        
        //scroll.setLayout(new ScrollPaneLayout());
        scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll.add(table);
        //scroll.setPreferredSize(400, 400);
        
        //table.setAutoscrolls(true);
        //table.createDefaultColumnsFromModel();
        //table.setSize(400, 400);
        //table.setPreferredScrollableViewportSize(new Dimension(250, 100));
        table.setPreferredSize(new Dimension(600,400));
        //table.set(columnsName);
        //table.getColumnName(0);
        //table.onChange();
        
        setLayout(new FlowLayout());
        
        
        //getContentPane().add(scroll);
        add(scroll);
        
        add(addButton);
        
        add(editButton);
        
        add(removeButton);
        
        exitButton.addActionListener(this);
        add(exitButton);
        
        add(table);
        
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        addWindowListener(this);
        setPreferredSize(new Dimension(800, 600));
        setSize(getPreferredSize());
        //pack();
        setVisible(true);
    }
    
    private void close(){
        
        String[] options = new String[]{"Выход", "Отмена"};
        if (JOptionPane.showOptionDialog(new JFrame(),"Подтверждение закрытия приложения.", "Выход", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]) == JOptionPane.OK_OPTION) System.exit(0);
        
    }
    
    @Override
    public void actionPerformed(ActionEvent ae){
        switch (ae.getActionCommand()){
            case "Добавить": new PersonCardFrame(new Person());
                             break;
            case "Выход": close();
                          break;
        }
    }

    @Override
    public void windowOpened(WindowEvent we) {
    }

    @Override
    public void windowClosing(WindowEvent we) {
        close();
    }

    @Override
    public void windowClosed(WindowEvent we) {
    }

    @Override
    public void windowIconified(WindowEvent we) {
    }

    @Override
    public void windowDeiconified(WindowEvent we) {
    }

    @Override
    public void windowActivated(WindowEvent we) {
    }

    @Override
    public void windowDeactivated(WindowEvent we) {
    }

    public void addPersonsList(PersonsList personsList) {
        table.setPersonsList(personsList);
//        int size = personsList.size();
//        persons = new String[size][][][];
        //System.out.println("---" +personsList.get(0));
//        persons = new String[personsList.getSize()][4];
//        for (int i = 0; i < personsList.getSize(); i++){
//            //for(int j = 0; j < 4; j++){
//            Person p = (Person)personsList.get(i);
//            persons[i][0] = p.getSurName();
//            persons[i][1] = p.getFirstName();
//            persons[i][2] = p.getSecondName();
//            persons[i][3] = p.getFunction();
//            //table.add(p.getSurName());
//            //System.out.println("\"" + personsList.get(i) + "\"");
//            //}
//        }
//        table = new JTable(persons, columnsName);
    }

}
